import { setGlobal } from '@swda/utils'

// 神魔异世录（敌人图鉴）
function Scene_EnemyBook() {
    this.initialize.apply(this, arguments);	
};

Scene_EnemyBook.prototype = Object.create(Scene_Base.prototype);
Scene_EnemyBook.prototype.constructor = Scene_EnemyBook;

Scene_EnemyBook.prototype.initialize = function() {
    Scene_Base.prototype.initialize.call(this);
};

Scene_EnemyBook.prototype.create = function() {
    Scene_Base.prototype.create.call(this);
    this.createBackground();
    this.createWindowLayer();
    this.createWindows();
};

Scene_EnemyBook.prototype.createBackground = function() {
    
    Scene_MenuBase.prototype.createBackground.call(this);
    this._backgroundSprite = new Sprite();
    this._backgroundSprite.bitmap = 
            ImageManager.loadBitmap('img/system/UI/', 'MenuBg_EnemyBook');
    this.addChild(this._backgroundSprite);
};

Scene_EnemyBook.prototype.createWindows = function() {

    this.createCategoryWindow();        //敌人种族
    this.createInfoWindow();		    //敌人信息
    this.createListWindow();		    //敌人列表
    this._listWindow.opacity = 0;
    this._categoryWindow.opacity = 0;
    this._infoWindow.opacity = 0;
    this._categoryWindow.refresh();
    this.selectLast();
    //this._infoWindow.hide();
};


Scene_EnemyBook.prototype.createCategoryWindow = function () {
    this._categoryWindow = new Window_EnemyCategory();

    //this._categoryWindow.setHandler('ok',     this.onCategoryOk.bind(this));
    this._categoryWindow.setHandler('cancel', this.popScene.bind(this));
    this.addWindow(this._categoryWindow);

    const [x, y] = [0, 32];
    this._categoryWindow.x = x;
    this._categoryWindow.y = y;
    //this._categoryWindow.width = width;

    this._categoryWindow.setHelpWindow(this._helpWindow);

    this._categoryWindow.refresh();
};


Scene_EnemyBook.prototype.createListWindow = function() {
    this._listWindow = new Window_EnemyList(40,96,340,588);
    this._listWindow.setHandler('cancel', this.onEnemyCancel.bind(this));
    this._listWindow._scrollbar.setOffset(5)
    this.listenListWindowEvents();
    this.addWindow(this._listWindow);
    this._categoryWindow.setListWindow(this._listWindow);
    this._listWindow.activate();
    //this._listWindow.select(0);

};

Scene_EnemyBook.prototype.createInfoWindow = function() {
    this._infoWindow = new Window_EnemyInfo();
    this.addWindow(this._infoWindow);
    this._categoryWindow.setInfoWindow(this._infoWindow);
};
/*
Scene_EnemyBook.prototype.onCategoryOk = function() {
    this._listWindow.activate();
    //this._categoryWindow.deactivate();
    //this._categoryWindow.activate();
    this._categoryWindow.refresh();
    this._listWindow.refresh();
    this._infoWindow.refresh();
    this._infoWindow.show();
};
*/
Scene_EnemyBook.prototype.onListOk = function() {
    this._listWindow.activate();
};

Scene_EnemyBook.prototype.onEnemyCancel = function() {
    this._listWindow.deselect();
    this._categoryWindow.activate();
};



Scene_EnemyBook.prototype.listenListWindowEvents = function () {
    this._listWindow.setHandler('ok', this.onListOk.bind(this));
    this._listWindow.setHandler('cancel', this.onEnemyCancel.bind(this));
    this._listWindow.$on('select', this.updateInfoWindow.bind(this));
    this._listWindow.$on('deselect', this.hideInfoWindow.bind(this));
}

Scene_EnemyBook.prototype.updateInfoWindow = function () {
    if (this._listWindow.index()>=0)
        this._infoWindow.setEnemy(this._listWindow._data[this._listWindow.index()]);
    else
        this._infoWindow.setEnemy(null);
}

Scene_EnemyBook.prototype.hideInfoWindow = function () {
    if (!this._infoWindow) return;
    this._infoWindow.hide();
}


Scene_EnemyBook.prototype.selectLast = function() {
    if(ConfigManager.commandRemember){
        if(!!$gameParty.lastEnemyBookEnemy()){
            const [classId, enemyId] = $gameParty.lastEnemyBookEnemy();
            this._categoryWindow.select(classId - 6)
            const id = this._listWindow._data.indexOf($dataEnemies[enemyId])
            this._listWindow.select( id >= 0 ? id : 0)
            return;
        }
    }
    // 非记忆指令情况默认选第一个
    this._categoryWindow.select(0)
    this._listWindow.select(0)
}

setGlobal('Scene_EnemyBook', Scene_EnemyBook)